<template>
  <div class="onePageTable">
  <div class="commonTablePanel openatc-role">
    <div class="common-table-header">
    <div class="common-table-title-container">
      <div class="common-table-title">{{$t('auth.route.permission')}}</div>
    </div>
    <div class="filter-container">
      <el-form>
        <el-form-item>
          <el-input
            :placeholder="$t('auth.route.permission')"
            @keyup.enter.native="handleFilter"
            v-model="schfilter"
            style="width: 200px;"
          />
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >{{$t('auth.common.search')}}</el-button>
          <el-button
            type="primary"
            v-if="isOpen==true"
            icon="el-icon-circle-plus-outline"
            @click="openAllPremission"
          >{{$t('auth.permission.expandall')}}</el-button>
          <el-button
            type="primary"
            v-else
            icon="el-icon-remove-outline"
            @click="openAllPremission"
          >{{$t('auth.permission.collapseall')}}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
    <!-- <tree-table
      :data="data"
      border
    /> -->
    <div class="atc-table">
      <permission-tree-table :data="data" />
    </div>
  </div>
</div>
</template>
<script>
/* eslint-disable */
import treeTable from "../../components/TreeTable";
import PermissionTreeTable from './components/PermissionTreeTable'
import {
  GetPermissionList,
  GetPermissionById,
  AddPermission,
  UpdatePermission,
  DeletePermission
} from "../../api/permission";
import { elTreeModel } from "../../model/tree/eltreemodel";
import { getMessageByCode } from '../../utils/responseMessage'

export default {
  name: "permission",
  components: { treeTable, PermissionTreeTable },
  data () {
    return {
      data: [],
      schfilter: "",
      allPermission: [],
      isOpen: true,
      new_id: [],
      delete_id: []
    };
  },
  watch: {
    schfilter: function (curVal, oldVal) {
      if (curVal === "") {
        this.getList();
      } else if (oldVal !== curVal) {
        this.new_id = [];
        this.delete_id = [];
      }
    },
    deep: true
  },
  created () {
    this.getList();
  },
  methods: {
    getList (list) {
      //查询所有权
      GetPermissionList().then(data => {
        if (data.data.code === '401') { // 判断用户是否在其他地方被踢出
          window.location.reload()
          return
        }
        if (data.data.success !== true) {
          this.$message.error(getMessageByCode(data.data.code, this.$i18n.locale))
          return
        }
        this.allPermission = data.data.data;
        for (let item of this.allPermission) {
          item.orgnization_code = item.permission_code
          item.orgnization_no = item.id
          item.subregion_status = 0
        }
        let _this = this
        this.allPermission = this.allPermission.map(item => {
          let code = 'auth.permission.' + item.permission_code
          let description = _this.$t(code)
          item.description = description
          return item
        })
        this.data = elTreeModel.CreateTreeByPath(this.allPermission);
        console.log('data', this.data)
        if (this.schfilter !== "") {
          this.handleFilter();
        } else if (!this.isOpen) {
          const funcSearchCheck = node => {
            node.expand = "true";
          };
          for (let curNode of this.data) {
            for (let node of curNode.g_travel()) {
              funcSearchCheck(node);
            }
          }
        } else if (list) {
          const funcSetCheck = node => {
            for (let exp of list) {
              if (exp === node.id) {
                node.expand = "true";
              }
            }
          };
          for (let curNode of this.data) {
            for (let node of curNode.g_travel()) {
              funcSetCheck(node);
            }
          }
        }
      });
    },
    handleFilter () {
      if (this.schfilter === "") {
        this.isOpen = false;
      }
      let search_id = [];
      let serrch_pre = this.allPermission;
      let num = 0;
      this.data = elTreeModel.CreateTreeByPath(serrch_pre);
      const funcSearch = node => {
        if (
          node.label
            .toUpperCase()
            .indexOf(this.schfilter.toUpperCase()) != -1
        ) {
          search_id.push(node.id);
          num++;
          if (node.parent) {
            funcSearchParentid(node.parent);
          }
        }
      };
      const funcSearchParentid = node => {
        search_id.push(node.id);
        if (node.parent) {
          funcSearchParentid(node.parent);
        }
      };
      for (let curNode of this.data) {
        for (let node of curNode.g_travel()) {
          funcSearch(node);
        }
        search_id.push(1);
        search_id = search_id.concat(this.new_id);
        for (let deleteId of this.delete_id) {
          search_id.splice(deleteId);
        }
      }

      serrch_pre = serrch_pre.filter(function (item) {
        return search_id.includes(item.id);
      });
      if (num > 0) {
        //判断搜索结果是否为空
        this.data = elTreeModel.CreateTreeByPath(serrch_pre);
        const funcSearchCheck = node => {
          for (let exp of search_id) {
            if (exp === node.id) {
              node.expand = "true";
            }
          }
        };
        for (let curNode of this.data) {
          for (let node of curNode.g_travel()) {
            funcSearchCheck(node);
          }
        }
      } else {
        this.data = [];
      }
    },
    openAllPremission () {
      if (this.isOpen && this.schfilter !== "") {
        this.handleFilter();
        this.isOpen = !this.isOpen;
        return;
      }
      this.new_id = [];
      this.delete_id = [];
      this.data = elTreeModel.CreateTreeByPath(this.allPermission);
      const funcSearchCheck = node => {
        node.expand = this.isOpen;
      };
      for (let curNode of this.data) {
        for (let node of curNode.g_travel()) {
          funcSearchCheck(node);
        }
      }
      this.isOpen = !this.isOpen;
    }
  }
};
</script>
